﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Diagrams.Core;
using Telerik.Windows.Controls;

namespace CalculationDemo
{
    public partial class WaterDistribution : ChildWindow
    {

        public List<Unit> unitListForTest;
        //NewCalculation calculation = new NewCalculation();

        public List<Unit> UnitListForTest
        {
            get { return unitListForTest; }
            set { unitListForTest = value; }
        }

        public WaterDistribution()
        {
            InitializeComponent();
            //UnitListForTest = calculation.AllUnitList.Where(p => p.FluidType == FIFPConstants.WATER).ToList();
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            //calculation.LoadUnitsS4WithStrings();            
            this.DataContext = this;
        }

        public void UpdateChart()
        {
            ConnectionCollection cc = fieldDiagram.Connections;
            ShapeCollection sc = fieldDiagram.Shapes;
            foreach (Unit unit in UnitListForTest)
            {
                if (unit.IsBottleNeck == true)
                {
                    foreach (RadDiagramConnection c in cc)
                    {
                        if (unit.UnitName == c.Name)
                        {
                            c.Stroke = new SolidColorBrush(Colors.Red);
                        }
                    }
                }
                else
                {
                    foreach (RadDiagramConnection c in cc)
                    {
                        if (unit.UnitName == c.Name)
                        {
                            c.Stroke = new SolidColorBrush(Colors.Black);
                        }
                    }
                }

                foreach (RadDiagramConnection c in cc)
                {
                    if (unit.UnitName == c.Name)
                    {
                        if (unit.UnitType == FIFPConstants.UNITTYPE_PIPELINE)
                            c.Content = unit.UnitName + "   C : " + unit.Capacity + " EC : " + unit.EffectiveCapacity;
                        else if (unit.UnitType == FIFPConstants.UNITTYPE_WELL)
                            c.Content = unit.UnitName + "   C : " + unit.Capacity + " RC : " + unit.RealCapacity;
                    }
                }

                foreach (RadDiagramShape s in sc)
                {
                    if (unit.UnitName == s.Name)
                    {
                        if (unit.UnitType != FIFPConstants.UNITTYPE_RADIAL)
                            s.Content = unit.UnitName + "             EC : " + unit.EffectiveCapacity;
                        else
                            s.Content = unit.UnitName + "             C : " + unit.Capacity + "             EC : " + unit.EffectiveCapacity;
                    }
                }
            }
        }

        private void CalculateEC(object sender, RoutedEventArgs e)
        {
            //pre calculate
            foreach (Unit tu in UnitListForTest)
            {
                tu.EffectiveCapacity = 0;
                tu.RealCapacity = 0;
                tu.IsBottleNeck = false;

                if (tu.UnitType == FIFPConstants.UNITTYPE_TOWER || tu.UnitType == FIFPConstants.UNITTYPE_WELL || tu.UnitType == FIFPConstants.UNITTYPE_STRING)
                    tu.Capacity = 0;                
            }

            //calculate the root node effective capacity
            Unit u = UnitListForTest[0];

            //DateTime ct = Convert.ToDateTime(CalculateDate.Text);

            //calculation.CalculateWaterStringCurrentDayRealCapacity(ct);            

            //post calculate, update the chart
            ConnectionCollection cc = fieldDiagram.Connections;
            ShapeCollection sc = fieldDiagram.Shapes;
            foreach (Unit unit in UnitListForTest)
            {
                if (unit.IsBottleNeck == true)
                {
                    foreach (RadDiagramConnection c in cc)
                    {
                        if (unit.UnitName == c.Name)
                        {
                            c.Stroke = new SolidColorBrush(Colors.Red);
                        }
                    }
                }
                else
                {
                    foreach (RadDiagramConnection c in cc)
                    {
                        if (unit.UnitName == c.Name)
                        {
                            c.Stroke = new SolidColorBrush(Colors.Black);
                        }
                    }
                }               

                foreach (RadDiagramConnection c in cc)
                {
                        if (unit.UnitName == c.Name)
                        {
                            if (unit.UnitType == FIFPConstants.UNITTYPE_PIPELINE)
                                c.Content = unit.UnitName + "   C : " + unit.Capacity + " EC : " + unit.EffectiveCapacity;
                            else if (unit.UnitType == FIFPConstants.UNITTYPE_WELL)
                                c.Content = unit.UnitName + "   C : " + unit.Capacity + " RC : " + unit.RealCapacity;
                        }
                }

                foreach (RadDiagramShape s in sc)
                {
                    if (unit.UnitName == s.Name)
                    {
                        if(unit.UnitType != FIFPConstants.UNITTYPE_RADIAL)
                            s.Content = unit.UnitName + "             EC : " + unit.EffectiveCapacity;
                        else
                            s.Content = unit.UnitName + "             C : " + unit.Capacity +"             EC : " + unit.EffectiveCapacity;
                    }
                }
            }                     
        }

        private void CalculateDistribution(object sender, RoutedEventArgs e)
        {
            //Unit u = UnitListForTest[0];
            //calculation.DistributetoString(u, UnitListForTest);            
        }

    }
}
